package com.digitalchina.lxnsy.sy.jgc;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.webkit.JavascriptInterface;

import com.digitalchina.lxnsy.api.BusinessFile;
import com.lzy.imagepicker.ImagePicker;
import com.digitalchina.lxnsy.base.BaseApplication;
import com.digitalchina.lxnsy.base.BaseHtmlActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;


import org.json.JSONException;

import java.util.ArrayList;


public class AddRawMilkConnectActivity extends BaseHtmlActivity {
    private ImagePicker imagePicker;
    public static final int REQUEST_CODE_SELECT = 100;
    private int tag = 0;


    @Override
    protected void loadHtml() {
        webView.addJavascriptObject(new JsApi(), null);

        loadUrl("addRawMilkConnect");
       String title= getIntent().getStringExtra("title");
        setTitle(title+"生鲜乳交接");


        //打开选择,本次允许选择的数量
        imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new BaseApplication.GlideImageLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);  //显示拍照按钮
        imagePicker.setCrop(false);        //允许裁剪（单选才有效）
        imagePicker.setMultiMode(false);    //是否多选模式


    }

//    @Override
//    public void onResume() {
//        super.onResume();
//        if (tag > 0) {
//            callJsMethod("refresh");
//        }
//        tag++;
//
//    }

    @Override
    public void rightOnClick() {



    }


    @Override
    public void setHandler() {

        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG.MSG_FILE_UPLOAD_SUCCESS: // 上传头像图
                        dismissDialog();
                        try {
                            String  value = new org.json.JSONObject(msg.obj.toString()).getString("value");
                          //  showToast(value);

                            callJsMethod("showImg", new String[]{value});
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        break;
                    case MSG.MSG_FILE_UPLOAD_FIELD:
                        dismissDialog();
                        showToast(msg.obj.toString());
                        break;
                }
                return false;
            }
        });

    }


    /**
     * 跳转回调
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            //添加图片返回
            if (data != null && requestCode == REQUEST_CODE_SELECT) {
                showDialog("上传中,请稍候!");
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                BusinessFile.imageCompressAndUpload(this, images.get(0).path.toString(), mHandler);
            }
        }else {
            callJsMethod("refresh");
        }
    }



    public class JsApi {
        /**
         * 修改头像跳转至自定义相册
         */
        @JavascriptInterface
        public void selectImage(Object msg) {
            Intent intent = new Intent(getApplicationContext(), ImageGridActivity.class);
            startActivityForResult(intent, REQUEST_CODE_SELECT);

        }


        @JavascriptInterface
        public void selectSalesBatches(Object msg) {
            Intent intent = new Intent(getApplicationContext(), SelectSalesBatchesActivity.class);
            startActivityForResult(intent, REQUEST_CODE_SELECT);
        }







    }
}
